package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.ServiceUsersBo;
import com.bigdata.service.domain.vo.ServiceUsersVo;
import com.bigdata.service.domain.ServiceUsers;
import com.bigdata.service.mapper.ServiceUsersMapper;
import com.bigdata.service.service.IServiceUsersService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RequiredArgsConstructor
@Service
public class ServiceUsersServiceImpl implements IServiceUsersService {

    private final ServiceUsersMapper baseMapper;

    /**
     * 查询用户信息
     */
    @Override
    public ServiceUsersVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<ServiceUsersVo> queryPageList(ServiceUsersBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServiceUsers> lqw = buildQueryWrapper(bo);
        Page<ServiceUsersVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<ServiceUsersVo> queryList(ServiceUsersBo bo) {
        LambdaQueryWrapper<ServiceUsers> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServiceUsers> buildQueryWrapper(ServiceUsersBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServiceUsers> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAccountType()), ServiceUsers::getAccountType, bo.getAccountType());
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), ServiceUsers::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getPasswordHash()), ServiceUsers::getPasswordHash, bo.getPasswordHash());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), ServiceUsers::getRealName, bo.getRealName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), ServiceUsers::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), ServiceUsers::getEmail, bo.getEmail());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), ServiceUsers::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getPosition()), ServiceUsers::getPosition, bo.getPosition());
        lqw.eq(StringUtils.isNotBlank(bo.getMemberType()), ServiceUsers::getMemberType, bo.getMemberType());
        lqw.eq(bo.getMemberEndTime() != null, ServiceUsers::getMemberEndTime, bo.getMemberEndTime());
        lqw.orderByDesc(ServiceUsers::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insertByBo(ServiceUsersBo bo) {
        ServiceUsers add = BeanUtil.toBean(bo, ServiceUsers.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateByBo(ServiceUsersBo bo) {
        ServiceUsers update = BeanUtil.toBean(bo, ServiceUsers.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ServiceUsers entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
